package br.com.fatecpg.minhasfinancas.sqlite;

import java.util.LinkedList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import br.com.fatecpg.minhasfinancas.model.CartaoModel;

@SuppressLint("SimpleDateFormat")
public class CartaoSqlite extends SQLiteOpenHelper {

	public CartaoSqlite(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	private static final int DATABASE_VERSION = 4;
	private static final String DATABASE_NAME = "financas";

	@Override
	public void onCreate(SQLiteDatabase db) {

		String query = "" + "CREATE TABLE CARTAO("
				+ "cd_cartao INTEGER PRIMARY KEY NOT NULL, "
				+ "nm_bandeira VARCHAR NULL, " + "dd_vencimento VARCHAR NULL, "
				+ "dc_cartao VARCHAR NULL, " + "dc_tipo VARCHAR NULL );";
		db.execSQL(query);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// db.execSQL("DROP TABLE IF EXISTS tb_usuario");
		this.onCreate(db);
	}

	private static final String TABLE_CARTAO = "cartao";
	private static final String KEY_ID = "cd_cartao";
	private static final String KEY_BANDEIRA = "nm_bandeira";
	private static final String KEY_VENCIMENTO = "dd_vencimento";
	private static final String KEY_DESCRICAO = "dc_cartao";
	private static final String KEY_TIPO = "dc_tipo";

	private static final String[] COLUMNS = { KEY_ID, KEY_BANDEIRA,
			KEY_VENCIMENTO, KEY_DESCRICAO, KEY_TIPO };

	public void Insert(CartaoModel model) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(KEY_BANDEIRA, model.getBandeira());
		values.put(KEY_VENCIMENTO, model.getDiaVencimento());
		values.put(KEY_DESCRICAO, model.getDescricao());
		values.put(KEY_TIPO, model.getTipo());

		db.insert(TABLE_CARTAO, null, values);

		db.close();
	}

	public CartaoModel getCartao(int id) {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(
				TABLE_CARTAO, // a. table
				COLUMNS, " id = ?", new String[] { String.valueOf(id) }, null,
				null, // f. having
				null, // g. order by
				null); // h. limit

		if (cursor != null)
			cursor.moveToFirst();
		CartaoModel model = new CartaoModel();
		model.setId(Integer.parseInt(cursor.getString(0)));
		model.setBandeira(cursor.getString(1));
		model.setDiaVencimento(Integer.parseInt(cursor.getString(2)));
		model.setDescricao(cursor.getString(3));
		model.setTipo(cursor.getString(4));
		return model;
	}

	public List<CartaoModel> getListaCartao() {
		List<CartaoModel> cartoes = new LinkedList<CartaoModel>();
		String query = "SELECT  * FROM " + TABLE_CARTAO;
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(query, null);
		CartaoModel cartao = null;
		if (cursor.moveToFirst()) {
			do {
				cartao = new CartaoModel();
				cartao.setId(Integer.parseInt(cursor.getString(0)));
				cartao.setBandeira(cursor.getString(1));
				cartao.setDiaVencimento(Integer.parseInt(cursor.getString(2)));
				cartao.setDescricao(cursor.getString(3));
				cartao.setTipo(cursor.getString(4));
				cartoes.add(cartao);
			} while (cursor.moveToNext());
		}

		return cartoes;
	}

	public int update(CartaoModel model) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(KEY_BANDEIRA, model.getBandeira());
		values.put(KEY_VENCIMENTO, model.getDiaVencimento());
		values.put(KEY_DESCRICAO, model.getDescricao());
		values.put(KEY_TIPO, model.getTipo());
		int i = db.update(TABLE_CARTAO, values, KEY_ID + " = ?", // selections
				new String[] { String.valueOf(model.getId()) });
		db.close();
		return i;
	}

	public List<String> getListaCartaoToString() {
		List<String> despesas = new LinkedList<String>();
		String query = "SELECT  dc_descricao FROM " + TABLE_CARTAO;
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(query, null);
		if (cursor.moveToFirst()) {
			do {
				despesas.add(cursor.getString(0));
			} while (cursor.moveToNext());
		}
		return despesas;
	}

	public void delete(CartaoModel model) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(TABLE_CARTAO, KEY_ID + " = ?",
				new String[] { String.valueOf(model.getId()) });
		db.close();
	}
}
